// pages/search/search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
       list:[],//列表
       isshow: true,//搜索状态
       searchtext:'',//搜索词
       page: 1,//页数
       viewshow:true,//绚烂状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       var that=this
       // 加载热门搜索词  
       that.hotsearch()
       that.read()

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
       var that = this
          var page = that.data.page
          page++
          that.setData({
               page: page
          })
           //     首页分页加载
          that.search()
  
  },
  // 热门搜索 
  hotsearch:function(){
       var that = this
       wx.request({
            url: app.data.apiurl + '/qupu/search/keywordList',
            dataType: 'json',
            method: "GET",
            header: {
                 'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                 //console.log(res.data.data)
                 that.setData({
                      hotlist: res.data.data
                 })
            }
       })
  },
//   取消搜索
 cancel:function(){
      wx.navigateBack({
           url: '/pages/index/index',
      })
 },
  // 点击热门搜索
 hotlisi:function(e){
      //console.log(e.target.dataset.text)
      var that = this
      that.setData({
           searchtext: e.target.dataset.text
      })
      that.search()
      that.Storage()
 },
 // 点击搜索
 searchbut:function(){
      var that = this
      that.search()
      that.Storage()
 },
 // 点击历史
 cliklisi:function(e){
      var that = this
      that.setData({
           searchtext: e.target.dataset.text
      })
      that.search()
 },
 //   历史清除
 quanclcer: function () {
      wx.setStorageSync("set", )
      this.setData({
           historylist: ''
      })
 },
 //    单独删除历史
 clcer: function (e) {
      //console.log(e.currentTarget.dataset.index)
      var index = e.currentTarget.dataset.index
      var tids = wx.getStorageSync('set');//读取
      var lisi = tids.split("#");
      for (var i = 0; i < lisi.length; i++) {
           if (lisi[i] == "" || typeof (lisi[i]) == "undefined") {
                lisi.splice(i, 1);
                i = i - 1;
           }
      }
      lisi.splice(index, 1);
      var newlisi = ''
      for (var j = 0; j < lisi.length; j++) {
           newlisi += lisi[j] + '#'
      }
      wx.setStorageSync('set', newlisi)
      this.setData({
           historylist: lisi
      })

 },
//  监听键盘搜索
 searchBtn: function () {
      var that = this
      that.search()
 },
 //   获取搜索框的值
 inputing: function (e) {
      if (e.detail.value == "") {
           this.setData({
                isshow: true
           })
      } else {
           this.setData({
                isshow: false,
                searchtext: e.detail.value
           })
           //console.log(e.detail.value)
      }
 },
// 搜索请求
 search:function(){
      var that=this
      var searchtext = that.data.searchtext
      wx.request({
           url: app.data.apiurl + '/qupu/search',
           dataType: 'json',
           method: "POST",
           header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
           },
           data: {
                keyword: searchtext,
                pageSize: 12
           },
           success: function (res) {
                //console.log(res.data.data)
                var reslist = res.data.data
                var list = that.data.list;
                if (reslist.length == 0) {
                     wx.showToast({
                          title: '暂无数据,敬请期待！',
                          icon: 'none',
                          duration: 2000
                     })
                } else {
                     for (var i = 0; i < reslist.length; i++) {
                          list.push(reslist[i]);
                     }
                     //console.log(list)
                     that.setData({
                          list: list,
                          hidden: true,
                          viewshow:false
                     })
                }

           }
      })
 },
 // 储存
 Storage: function () {
      var that = this
      var text = that.data.searchtext
      var tids = wx.getStorageSync('set');//读取
      var searchlist = "";
      var k = 0;
      try {
           var list = tids.split("#");
           for (var i = 0; i < list.length; i++) {
                if (list[i].length != 0) {
                     var c_id = list[i];
                     if (c_id == text) {
                          k = 1
                     }
                     searchlist += c_id + "#";
                }
           }
      } catch (e) {

      }

      if (k == 0) {
           searchlist += text + "#";
      }
      wx.setStorageSync('set', searchlist)//存
 },
//  读取储存
 read:function(){
      var that = this
      var tids = wx.getStorageSync('set');//读取
      var lisi = tids.split("#");
      for (var i = 0; i < lisi.length; i++) {
           if (lisi[i] == "" || typeof (lisi[i]) == "undefined") {
                lisi.splice(i, 1);
                i = i - 1;
           }
      }
      that.setData({
           historylist: lisi
      })
 }
})
